/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Adam Dickinson
 */
public class HTTPListener implements Runnable {

    private ServerSocket httpSocket;
    private Socket connectionSocket;

    public HTTPListener() throws Exception {
        httpSocket = new ServerSocket(80);

    }

    public String welcome() throws Exception {
        BufferedReader template = new BufferedReader(new FileReader("welcome_template.html"));
        String line = template.readLine();
        String page = "";

        while (line != null) {
            page += line;
            line = template.readLine();
        }
                
        page = page.replace("<?content>", "Welcome to email server");

        return page;
    }

    public String displayMessage(String user, int message) throws Exception {

        BufferedReader template = new BufferedReader(new FileReader("message_template.html"));
        String line = template.readLine();
        String page = "";


        while (line != null) {
            page += line;
            line = template.readLine();
        }

        UserMail usermail = UserMail.open(user);
        

        try {
        page = page.replace("<?from>", "Messages : " + usermail.size());
        page = page.replace("<?message>", usermail.find(message).toWebString());
        } catch (Exception ex) {
            page.replace("<?message>", "No such message");
        }
        
        return page;
    }
    
    public String displayEmails(String user) throws Exception {
        BufferedReader template = new BufferedReader(new FileReader("welcome_template.html"));
        String line = template.readLine();
        String page = "";

        while (line != null) {
            page += line;
            line = template.readLine();
        }
        String output;
        try {
            UserMail usermail = UserMail.open(user);
            output = usermail.toWebString();
        } catch (Exception ex) {
            output = "Invalid user";
        }
        
        page = page.replace("<?content>", output);

        return page;
    }
    
    public String httpGetMessage(String url) throws Exception {
        String ret = "";
        String urlParts[] = url.split("/");
       
        for (String bit : urlParts)
        
        switch (urlParts.length) {
            case 0:
                ret = welcome();
                break;
            case 2:
                ret = displayEmails(urlParts[1]);
                break;
            case 3:
                ret = displayMessage(urlParts[1], Integer.parseInt(urlParts[2]));
                break;
            default:
                ret += "403 error";
        }
        
        return ret;
    }

    @Override
    public void run() {

        while (true) {
            try {

                connectionSocket = httpSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream());

                String input = in.readLine();
                String[] command = input.split(" ");


                String output;
                if (command[0].equalsIgnoreCase("get"))
                    output = httpGetMessage(command[1]);
                else
                    output = "500 Bad command";

                out.write(output.getBytes());                
                out.close();
            } catch (Exception ex) {
               // System.out.println(ex.getMessage());
            }
        }
    }
}
